import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: {
      title: '首页',
      icon: 'dashboard',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '首页',
        icon: 'dashboard',
        roles: ['admin', 'editor'] // you can set roles in root nav
      }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: '案例组件', icon: 'example' },
    children: [
      {
        path: 'dragG6',
        name: 'dragG6',
        component: () => import('@/views/G6/index'),
        meta: { title: 'dragG6' }
      },
      {
        path: 'player',
        name: 'player',
        component: () => import('@/views/dplayerDemo/index'),
        meta: { title: 'player' }
      },
      {
        path: 'drag',
        name: 'drag',
        component: () => import('@/views/drag/index'),
        meta: { title: '测试' }
      },
      {
        path: 'nomal',
        name: 'Nomal',
        component: () => import('@/views/table/nomal'),
        meta: { title: '常规表格' }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/table/edit'),
        meta: { title: '可修改数据表格' }
      },
      {
        path: 'checkData',
        name: 'checkData',
        component: () => import('@/views/table/checkData'),
        meta: { title: '可选中表格' }
      },
      {
        path: 'comp',
        name: 'compTable',
        component: () => import('@/views/table/tableComp'),
        meta: { title: '组件化表格' }
      },
      {
        path: 'spanMethod',
        name: 'spanMethod',
        component: () => import('@/views/table/spanMethod'),
        meta: { title: '合并行或列' }
      },
      {
        path: 'org',
        name: 'Org',
        component: () => import('@/views/tree/org'),
        meta: { title: '组织结构的树' }
      },
      {
        path: 'check',
        name: 'Check',
        component: () => import('@/views/tree/check'),
        meta: { title: '可选中的树结构' }
      },
      {
        path: 'map',
        name: 'map',
        component: () => import('@/views/map/index'),
        meta: {
          title: '地图',
          roles: ['editor'] // you can set roles in root nav
        }
      },
      {
        path: 'editart',
        name: 'editart',
        component: () => import('@/views/components/editart'),
        meta: {
          title: '富文本编辑器',
          roles: ['editor'] // you can set roles in root nav
        }
      },
      {
        path: 'https://blog.csdn.net/If_CannewBoyFriend/article/details/108072059',
        meta: { title: 'dragtree' }
      },
      {
        path: 'https://blog.csdn.net/If_CannewBoyFriend/article/details/120850490',
        meta: { title: '级联选择器分页' }
      }
    ]
  },
  {
    path: '/form',
    component: Layout,
    meta: {
      title: '表单',
      icon: 'form',
      roles: ['editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: {
          title: '表单',
          roles: ['editor'] // you can set roles in root nav
        }
      }
    ]
  },
  {
    path: '/components',
    component: Layout,
    meta: {
      title: '组件',
      icon: 'form',
      roles: ['editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'charts',
        name: 'charts',
        component: () => import('@/views/components/echarts/index'),
        meta: {
          title: '图表组件',
          roles: ['editor'] // you can set roles in root nav
        }
      },
      {
        path: 'table',
        name: 'table',
        component: () => import('@/views/components/table/index'),
        meta: {
          title: '表格操作',
          roles: ['editor'] // you can set roles in root nav
        }
      }

    ]
  },

  /* {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '外部链接', icon: 'link' }
      }
    ]
  },*/

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
